package com.powernode.config;

import com.powernode.service.impl.PowerUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;

import java.security.KeyPair;

@Configuration
@EnableAuthorizationServer //授权服务
public class PowerAuthorizeConfiguration extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private PasswordEncoder passwordEncoder;
    /**
     *     配置oauth2的授权模式
     *     密码模式
     *     客户端模式
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        super.configure(clients);
        clients.inMemory()
                .withClient("web")
                .secret(passwordEncoder.encode("web-secret"))
                .scopes("powernode")
                .authorizedGrantTypes("password","refresh_token") //授权模式
                .accessTokenValiditySeconds(7*3600)
                .redirectUris("https://www.baidu.com")
                .and()
                .withClient("client")
                .secret(passwordEncoder.encode("client-secret"))
                .scopes("powernode")
                .authorizedGrantTypes("client_credentials") //授权模式
                .accessTokenValiditySeconds(Integer.MAX_VALUE)
                .redirectUris("https://www.baidu.com")
        ;
    }
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private PowerUserDetailsService powerUserDetailsService;
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        //super.configure(endpoints);
        endpoints.tokenStore(powerTokenStore())
                .authenticationManager(authenticationManager) //支持密码模式
                .userDetailsService(powerUserDetailsService)  //支持token刷新
                .accessTokenConverter(jwtConverter())
        ;
    }
    @Bean
    public TokenStore powerTokenStore() {
        return  new JwtTokenStore(jwtConverter());
    }
    @Bean
    public JwtAccessTokenConverter jwtConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        //私钥加密
        ClassPathResource resource = new ClassPathResource("cxs-jwt.jks");
        //设置私钥加密
        KeyStoreKeyFactory factory = new KeyStoreKeyFactory(resource,"cxs123".toCharArray());
        KeyPair pair  = factory.getKeyPair("cxs-jwt") ;
        jwtAccessTokenConverter.setKeyPair(pair);
        return jwtAccessTokenConverter;
    }
}
